<!doctype html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<body contenteditable="true">
    <p>
        To test interactively, drag selected text onto the iframe
        below and verify that a drag caret appears inside the frame.
    </p>
    <iframe id="subframe" style="border: 1px solid black;"
        srcdoc="<body contenteditable><span id='target'>There</span> should be a caret here --><br>See it?</body>"></iframe>
    <script>
        const t = async_test('Drag-In-Iframge');
        window.addEventListener('load', t.step_func_done(() => {
            var iframe = document.getElementById("subframe");
            var target = iframe.contentDocument.getElementById("target");
        
            var x1 = iframe.offsetLeft + target.offsetLeft + target.offsetWidth / 2;
            var x2 = iframe.offsetLeft + iframe.offsetWidth - 20;
            var y = iframe.offsetTop + target.offsetTop + target.offsetHeight / 2;

            iframe.contentWindow.getSelection().setBaseAndExtent(target, 0, target, 1);
        
            assert_own_property(window, 'eventSender');
            assert_own_property(window, 'internals');

            eventSender.mouseMoveTo(x1, y);
            eventSender.dragMode = false;
            eventSender.mouseDown();
            eventSender.leapForward(1000);
            eventSender.mouseMoveTo(x2, y);

            const caretInMainFrame = internals.getDragCaret();
            assert_equals(caretInMainFrame.anchorNode, null,
                          'No drag caret in main frame');

            const caretInIframe = iframe.contentWindow.internals.getDragCaret();
            assert_true(caretInIframe.isCollapsed,
                          'Caret in iframe should be collapsed');
            const textBeforeCaret= target.nextSibling;
            assert_equals(caretInIframe.anchorNode, textBeforeCaret);
            assert_equals(caretInIframe.anchorOffset, textBeforeCaret.length);

            // To avoid crash, we should release mouse button.
            eventSender.mouseUp();
        }));
    </script>
</body>
</html>
